package com.orange.demo;

import java.util.Arrays;

/**
 * @author HongHao
 * @since 2021-08-30 21:54
 */
public class Mp3Parser implements Parser {

    public final byte[] FORMAT = "MP3".getBytes();

    public final int FORMAT_LENGTH = FORMAT.length;

    static
    {
        try {
            ParserManager.registerParser(new Mp3Parser());
            System.out.println("register   Mp3Parser");
        }
        catch (Exception e) {
            throw new RuntimeException("Can't register parser!");
        }
    }


    @Override
    public Song parse(byte[] data) throws Exception {
        if (!isDataCompatible(data)) {
            throw new Exception("data format is wrong.");
        }
        return new Song("mp3", "你不是真正的快乐", "五月天", 220L);
    }

    private boolean isDataCompatible(byte[] data) {
        byte[] format = Arrays.copyOfRange(data, 0, FORMAT_LENGTH);
        return Arrays.equals(format, FORMAT);
    }


}
